﻿Imports System.Xml

Namespace Spreads
	Public Class Spread
		Public Sub New()
			Me.Children = New List(Of Child)
		End Sub

		Public Overrides Function ToString() As String
			Return Me.Self
		End Function

		Property Self As String

		Property FlattenerPreference As FlattenerPreference

		Property FlattenerOverride As SpreadFlattenerLevel?

		Property AllowPageShuffle As Boolean?

		Property ItemTransform As String

		Property ShowMasterItems As Boolean?

		Property PageCount As Integer

		Property BindingLocation As Integer

		Property PageTransitionType As PageTransitionTypeOptions?

		Property PageTransitionDirection As PageTransitionDirectionOptions

		Property PageTransitionDuration As PageTransitionDurationOptions

		Property Children As List(Of Child)

		Public Shared Function ReadXml(reader As XmlReader) As Spread
			Dim result As New Spread()

			result.Self = reader.GetAttribute("Self")
			result.FlattenerOverride = Parser.ParseEnum(Of SpreadFlattenerLevel)(reader.GetAttribute("FlattenerOverride"))
			result.AllowPageShuffle = Parser.ParseBoolean(reader.GetAttribute("AllowPageShuffle"))
			result.ItemTransform = reader.GetAttribute("ItemTransform")
			result.ShowMasterItems = Parser.ParseBoolean(reader.GetAttribute("ShowMasterItems"))
			result.PageCount = CInt(reader.GetAttribute("PageCount"))
			result.BindingLocation = CInt(reader.GetAttribute("BindingLocation"))
			result.PageTransitionType = Parser.ParseEnum(Of PageTransitionTypeOptions)(reader.GetAttribute("PageTransitionType"))
			result.PageTransitionDirection = Parser.ParseEnum(Of PageTransitionDirectionOptions)(reader.GetAttribute("PageTransitionDirection"))
			result.PageTransitionDuration = Parser.ParseEnum(Of PageTransitionDurationOptions)(reader.GetAttribute("PageTransitionDuration"))

			While reader.Read()
				If reader.NodeType = XmlNodeType.Element Then
					Select Case reader.Name
						Case "Properties"
							While reader.Read()
								Select Case reader.Name
									Case ""
									Case "Properties"
										If reader.NodeType = XmlNodeType.EndElement Then Exit While
									Case Else
										If reader.NodeType = XmlNodeType.Element Then
											Debug.WriteLine("Unrecognized element: {0} in element: {1}", reader.Name, "Spread - Properties")
										End If
								End Select
							End While
						Case "FlattenerPreference"
							result.FlattenerPreference = FlattenerPreference.ReadXml(reader)
						Case "Page"
							result.Children.Add(Page.ReadXml(reader))
						Case "TextFrame"
							result.Children.Add(TextFrame.ReadXml(reader))
						Case "Rectangle"
							result.Children.Add(Rectangle.ReadXml(reader))
						Case Else
							If reader.NodeType = XmlNodeType.Element Then
								Debug.WriteLine("Unrecognized element: {0} in element: {1}", reader.Name, "Spread")
							End If
					End Select
				End If
			End While

			Return result
		End Function
	End Class
End Namespace